package com.finger.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.eduservice.entity.EduTeacher;
import com.finger.eduservice.mapper.EduTeacherMapper;
import com.finger.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-21
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    // key可选属性，可以使用 SpEL 标签自定义缓存的key
    // value 缓存名，必填，它指定了你的缓存存放在哪块命名空间
    @Cacheable(key = "'indexTeacherList'",value = "teacher")
    @Override
    public List<EduTeacher> selectIndexTeacherList() {
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();

        eduTeacherQueryWrapper.orderByDesc("sort");

        eduTeacherQueryWrapper.last("limit 4");

        return baseMapper.selectList(eduTeacherQueryWrapper);
    }

    @Override
    public Map<String,Object> selectTeacherPageList(Page<EduTeacher> eduTeacherPage) {

        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();

        eduTeacherQueryWrapper.orderByDesc("sort");

        IPage<EduTeacher> eduTeacherIPage = this.page(eduTeacherPage, eduTeacherQueryWrapper);

        long pages = eduTeacherIPage.getPages();
        long current = eduTeacherIPage.getCurrent();
        List<EduTeacher> records = eduTeacherIPage.getRecords();
        long size = eduTeacherIPage.getSize();
        long total = eduTeacherIPage.getTotal();

        boolean hasPrevious = true;
        boolean hasNext = true;
        if(current==pages){
            hasNext = false;
        }
        if(current == 1){
            hasPrevious = false;
        }

        Map<String, Object> map = new HashMap<>();

        map.put("pages",pages);
        map.put("current",current);
        map.put("records",records);
        map.put("size",size);
        map.put("total",total);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }
}
